﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using Lvzhi.Core;
using Lvzhi.Entities;
using Lvzhi.Provide.LambdaExtension;
using Lvzhi.Provide.Rpg;

namespace Lvzhi.Website.Controllers
{

    public class PlantsController : Controller
    {
        /// <summary>
        /// 排序字段
        /// </summary>
        private enum OrderStrEnum
        {
            G_Time = 0,  //默认
            G_Sales = 1,  //销量
            G_Recom = 2,  //人气
            G_PriceDeposit = 3 //价格
        }
        private GoodsCore gCore = new GoodsCore();
        /// <summary>
        /// 商品列表
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <param name="od">排序方向：0:asc ,1:desc</param>
        /// <param name="sort">排序字段</param>
        /// <returns></returns>
        public ActionResult Index(int id = 0, int od = 0, int sort = 0)
        {
            Expression<Func<Core_Goods, Boolean>> expr = t => t.G_Status == 0;
            if (id > 0)
            {
                expr = expr.And(o => o.G_TypeId == id);
            }
            string odstr = Enum.GetName(typeof(OrderStrEnum), sort); //排序字段

            string direction = od == 0 ? "asc" : "descending"; //排序值
            var plants = gCore.LoadEntities(expr).OrderBy(odstr, direction).ToList();
            ViewBag.Cid = id;

            return View(plants);
        }

        /// <summary>
        /// 商品展示
        /// </summary>
        /// <returns></returns>
        public ActionResult Show(Guid id)
        {
            var goods = gCore.LoadEntity(o => o.G_Id == id && o.G_Status == 0);

            if (goods != null)
            {
                AttachmentCore aCore = new AttachmentCore();
                var attList = aCore.LoadEntities(o => o.A_PId == id).ToList();
                ViewBag.AttList = attList;
                return View(goods);
            }
            return RedirectToAction("index");

        }
        /// <summary>
        /// 新品
        /// </summary>
        /// <returns></returns>
        public ActionResult New()
        {
            var plants = gCore.LoadEntities(o => o.G_Status == 0).OrderByDescending(o => o.G_Time).Take(20).ToList();
            return View("index", plants);
        }
        /// <summary>
        /// 热销
        /// </summary>
        /// <returns></returns>
        public ActionResult Hot()
        {
            var plants = gCore.LoadEntities(o => o.G_Status == 0).OrderByDescending(o => o.G_Sales).Take(20).ToList();
            return View("index", plants);
        }
        /// <summary>
        /// 低价
        /// </summary>
        /// <returns></returns>
        public ActionResult Cheap()
        {
            var plants = gCore.LoadEntities(o => o.G_Status == 0).OrderBy(o => o.G_PriceDeposit).Take(20).ToList();
            return View("index", plants);
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search(string name)
        {
            if (!string.IsNullOrEmpty(name))
            {
                name = name.Trim();
                var plants = gCore.LoadEntities(o => o.G_Status == 0 && o.G_Name.Contains(name)).ToList();
                string historyKey = CookieHelper.GetCookieValue("idol_history");
                if (string.IsNullOrEmpty(historyKey) || historyKey.IndexOf(name + ",", StringComparison.CurrentCultureIgnoreCase) < 0)
                {
                    historyKey += name + ",";

                    CookieHelper.SetCookie("idol_history", historyKey);
                }

                return View(viewName: "Index", model: plants);
            }
            return RedirectToAction("Index");
        }
    }
}